package com.baselibs.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.antiphon.baselibs.R;


/**
 * 作者： XuDiWei
 * <p/>
 * 日期：2014/7/20  17:06.
 * <p/>
 * 文件描述:圆角ImageView
 *
 * @attr ref R.styleable.RoundImageView_degree 图片的角度（int值，不用给单位）
 *
 */
public class RoundImageView extends ImageView {

    /**
     * 圆角角度
     */
    private int degree;
    private Matrix mMatrix = new Matrix();
    private Paint mPaint = new Paint();

    public RoundImageView(Context context) {
        super(context);
    }

    public RoundImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView);
        degree = typedArray.getInt(R.styleable.RoundImageView_degree, 0);
        typedArray.recycle();
    }

    public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Bitmap roundBitmap = createRoundBitmap();
        if (null != roundBitmap) {
            canvas.drawBitmap(roundBitmap, mMatrix, mPaint);
            roundBitmap.recycle();
        }

        //这种方法好像有锯齿
//        setImageBitmap(roundBitmap);
//        super.onDraw(canvas);

    }

    private Bitmap createRoundBitmap() {
        //获取drawable然后转为bitmap
        Drawable drawable = getDrawable();
        if (null == drawable) {
            return null;
        }
        int intrinsicWidth = drawable.getIntrinsicWidth();
        int intrinsicHeight = drawable.getIntrinsicHeight();
        //创建空画板，用于把drawable画成bitmap
        Bitmap bitmap = Bitmap.createBitmap(intrinsicWidth, intrinsicHeight, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, intrinsicWidth, intrinsicHeight);
        drawable.draw(canvas);

        //转成圆角图片
        // 创建空画板，用于画一个圆角矩形和bitmap然后取交集     这里的宽高要用当前控件的宽高，不要用bitmap的宽高
        Bitmap roundBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvasRound = new Canvas(roundBitmap);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        //这里的宽高要用当前控件的宽高，不要用bitmap的宽高
        RectF rectF = new RectF(0, 0, getWidth(), getHeight());
        canvasRound.drawRoundRect(rectF, degree, degree, paint);
        //设置这个属性就可以获取后画的图与先画的矩形重叠部分显示后画的图形
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvasRound.drawBitmap(bitmap, null, rectF, paint);
        bitmap.recycle();
        return roundBitmap;

    }

    /**
     * 设置角度
     *
     * @param degree
     */
    public void setDegree(int degree) {
        this.degree = degree;
    }


}
